package com.example.localareanetworkchat.fragment.home.adapter

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.navigation.Navigation
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.example.localareanetworkchat.R
import com.example.localareanetworkchat.persistence.entity.ChatMessageItem
import com.example.localareanetworkchat.persistence.entity.User
import java.time.LocalDateTime
import java.time.ZoneId
import java.time.format.DateTimeFormatter
import java.util.*

class ChatMessageListAdapter(val context: Context): ListAdapter<ChatMessageItem, ChatMessageListAdapter.MyViewHolder>(object :
    DiffUtil.ItemCallback<ChatMessageItem>() {
    override fun areItemsTheSame(oldItem: ChatMessageItem, newItem: ChatMessageItem): Boolean {
        return oldItem.id == newItem.id
    }

    override fun areContentsTheSame(oldItem: ChatMessageItem, newItem: ChatMessageItem): Boolean {
        return oldItem == newItem
    }

})
{

    class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val tvChatMessageTime: TextView = itemView.findViewById(R.id.tv_chat_message_time)
        val tvChatMessageUserNameFirst: TextView = itemView.findViewById(R.id.tv_chat_message_user_name_first)
        val tvChatMessageUserName: TextView = itemView.findViewById(R.id.tv_chat_message_user_name)
        val tvChatMessageContent: TextView = itemView.findViewById(R.id.tv_chat_message_text)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        return when(viewType){
            ChatMessageItemViewType.TEXT_LEFT.ordinal -> {
                MyViewHolder(LayoutInflater.from(context).inflate(R.layout.chat_message_list_item_left_text, parent, false))
            }
            else -> {
                MyViewHolder(LayoutInflater.from(context).inflate(R.layout.chat_message_list_item_right_text, parent, false))
            }
        }
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {

        val localDateTime = LocalDateTime.ofInstant(Date(getItem(position).messageReceiveTimestamp).toInstant()
        , ZoneId.systemDefault())
        holder.tvChatMessageTime.text = localDateTime.format(DateTimeFormatter.ofPattern("yyyy:MM:dd HH:mm:ss"))

        holder.tvChatMessageUserNameFirst.text = getItem(position).userName.first().uppercase()
        holder.tvChatMessageUserName.text = getItem(position).userName
        holder.tvChatMessageContent.text = getItem(position).message

        holder.tvChatMessageUserNameFirst.setOnClickListener {
            val bundle = Bundle()
            bundle.putParcelable("user", User(0, getItem(position).userName, "", getItem(position).uid))
            Navigation.findNavController(it).navigate(R.id.chatGroupUserProfileFragment, bundle)
        }

    }

    override fun getItemViewType(position: Int): Int {
        val chatMessageItem = getItem(position)
        return if (chatMessageItem.userSelfSendFlag == 1){
            ChatMessageItemViewType.TEXT_LEFT.ordinal
        }else {
            ChatMessageItemViewType.TEXT_RIGHT.ordinal
        }
    }


    enum class ChatMessageItemViewType {
        TEXT_LEFT,TEXT_RIGHT
    }

}